<script setup lang="ts">
import {
  getConsultOrderPre,
  createConsultOrder,
  getConsultOrderPayUrl,
} from "@/services/consult";
import { getPatientDetail } from "@/services/user";
import useStore from "@/stores";
import type { ConsultOrderPreData } from "@/types/consult";
import type { Patient } from "@/types/user";
import { Dialog, Toast } from "vant";
import { onMounted, ref } from "vue";
import { onBeforeRouteLeave, useRoute, useRouter } from "vue-router";

const { consult } = useStore();
// 1. 查询预订单信息
const payInfo = ref<ConsultOrderPreData>({} as ConsultOrderPreData);
const loadData = async () => {
  const res = await getConsultOrderPre({
    type: consult.consult.type,
    illnessType: consult.consult.illnessType,
  });
  payInfo.value = res.data;
  // 设置默认优惠券
  consult.setCouponId(payInfo.value.couponId);
};
// 2. 查询患者信息
const patient = ref<Patient>();
const loadPatient = async () => {
  if (!consult.consult.patientId) return;
  const res = await getPatientDetail(consult.consult.patientId);
  patient.value = res.data;
};

onMounted(() => {
  loadData();
  loadPatient();
});

const agree = ref(false);
const show = ref(false);
// 支付方式
const paymentMethod = ref<0 | 1>();
const orderId = ref("");
const loading = ref(false);
const submit = async () => {
  if (!agree.value) return Toast("请勾选我已同意支付协议");
  loading.value = true;
  const res = await createConsultOrder(consult.consult);
  orderId.value = res.data.id;
  loading.value = false;
  consult.clear();
  // 打开
  show.value = true;
};
// 4. 进行支付
onBeforeRouteLeave(() => {
  if (orderId.value) return false;
});
const router = useRouter();
const onClose = () => {
  return Dialog.confirm({
    title: "关闭支付",
    message: "取消支付将无法获得医生回复，医生接诊名额有限，是否确认关闭？",
    cancelButtonText: "仍要关闭",
    confirmButtonText: "继续支付",
    confirmButtonColor: "var(--cp-primary)",
  })
    .then(() => {
      return false;
    })
    .catch(() => {
      orderId.value = "";
      router.push("/user/consult");
      return true;
    });
};
// 5. 跳转支付
const pay = async () => {
  if (!paymentMethod.value) return Toast("请选择支付方式");
  Toast.loading("跳转支付");
  const res = await getConsultOrderPayUrl({
    orderId: orderId.value,
    paymentMethod: paymentMethod.value,
    payCallback: "http://127.0.0.1:5173/room",
  });
  window.location.href = res.data.payUrl;
};
</script>

<template>
  <div class="consult-pay-page" v-if="payInfo">
    <cp-nav-bar title="支付" />
    <div class="pay-info">
      <p class="tit">图文问诊 {{ payInfo?.payment }} 元</p>
      <img class="img" src="@/assets/avatar-doctor.svg" />
      <p class="desc">
        <span>极速问诊</span>
        <span>自动分配医生</span>
      </p>
    </div>
    <van-cell-group>
      <van-cell title="优惠券" :value="`-¥${payInfo.couponDeduction}`" />
      <van-cell title="积分抵扣" :value="`-¥${payInfo.pointDeduction}`" />
      <van-cell
        title="实付款"
        :value="`¥${payInfo.actualPayment}`"
        class="pay-price"
      />
    </van-cell-group>
    <div class="pay-space"></div>
    <van-cell-group>
      <van-cell
        title="患者信息"
        :value="`${patient?.name} | ${patient?.genderValue} | ${patient?.age}岁`"
      ></van-cell>
      <van-cell
        title="病情描述"
        :label="consult.consult.illnessDesc"
      ></van-cell>
    </van-cell-group>
    <div class="pay-schema">
      <van-checkbox v-model="agree"
        >我已同意 <span class="text">支付协议</span></van-checkbox
      >
    </div>
    <van-submit-bar
      button-type="primary"
      :price="payInfo.actualPayment * 100"
      button-text="立即支付"
      text-align="left"
      @click="submit"
    />
    <!-- 支付弹层 -->
    <cp-pay-sheet
      v-model:show="show"
      :order-id="orderId"
      :actualPayment="payInfo.actualPayment"
      :onClose="onClose"
    />
  </div>
</template>

<style lang="scss" scoped>
.consult-pay-page {
  padding: 46px 0 50px 0;
  .pay-info {
    display: flex;
    padding: 15px;
    flex-wrap: wrap;
    align-items: center;
    .tit {
      width: 100%;
      font-size: 16px;
      margin-bottom: 10px;
    }
    .img {
      margin-right: 10px;
      width: 38px;
      height: 38px;
      border-radius: 4px;
      overflow: hidden;
    }
    .desc {
      flex: 1;
      > span {
        display: block;
        color: var(--cp-tag);
        &:first-child {
          font-size: 16px;
          color: var(--cp-text2);
        }
      }
    }
  }
  .pay-price {
    ::v-deep() {
      .vam-cell__title {
        font-size: 16px;
      }
      .van-cell__value {
        font-size: 16px;
        color: var(--cp-price);
      }
    }
  }
  .pay-space {
    height: 12px;
    background-color: var(--cp-bg);
  }
  .pay-schema {
    height: 56px;
    display: flex;
    align-items: center;
    justify-content: center;
    .text {
      color: var(--cp-primary);
    }
  }
  ::v-deep() {
    .van-submit-bar__button {
      font-weight: normal;
      width: 160px;
    }
  }
}
// 支付弹层样式
.pay-type {
  .amount {
    padding: 20px;
    text-align: center;
    font-size: 16px;
    font-weight: bold;
  }
  .btn {
    padding: 15px;
  }
  .van-cell {
    align-items: center;
    .cp-icon {
      margin-right: 10px;
      font-size: 18px;
    }
    .van-checkbox :deep(.van-checkbox__icon) {
      font-size: 16px;
    }
  }
}
</style>
